package com.dms.modules.order.event;

import com.dms.modules.message.dto.BusinessMessageDTO;
import com.dms.modules.message.enums.BusinessMessageType;
import com.dms.modules.message.service.BusinessMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
public class OrderEventListener {

    @Autowired
    private BusinessMessageService businessMessageService;

    @EventListener
    public void handleOrderEvent(OrderEvent event) {
        switch (event.getEventType()) {
            case "ORDER_CREATED":
                handleOrderCreated(event);
                break;
            case "ORDER_PAID":
                handleOrderPaid(event);
                break;
            case "ORDER_SHIPPED":
                handleOrderShipped(event);
                break;
            case "ORDER_COMPLETED":
                handleOrderCompleted(event);
                break;
            case "ORDER_CANCELLED":
                handleOrderCancelled(event);
                break;
        }
    }

    private void handleOrderCreated(OrderEvent event) {
        BusinessMessageDTO message = new BusinessMessageDTO();
        message.setUserId((Long) event.getData());
        message.setType(BusinessMessageType.ORDER_CREATED);
        businessMessageService.sendMessage(message);
    }

    private void handleOrderPaid(OrderEvent event) {
        BusinessMessageDTO message = new BusinessMessageDTO();
        message.setUserId((Long) event.getData());
        message.setType(BusinessMessageType.ORDER_PAID);
        businessMessageService.sendMessage(message);
    }

    private void handleOrderShipped(OrderEvent event) {
        BusinessMessageDTO message = new BusinessMessageDTO();
        message.setUserId((Long) event.getData());
        message.setType(BusinessMessageType.ORDER_SHIPPED);
        businessMessageService.sendMessage(message);
    }

    private void handleOrderCompleted(OrderEvent event) {
        BusinessMessageDTO message = new BusinessMessageDTO();
        message.setUserId((Long) event.getData());
        message.setType(BusinessMessageType.ORDER_COMPLETED);
        businessMessageService.sendMessage(message);
    }

    private void handleOrderCancelled(OrderEvent event) {
        BusinessMessageDTO message = new BusinessMessageDTO();
        message.setUserId((Long) event.getData());
        message.setType(BusinessMessageType.ORDER_CANCELLED);
        businessMessageService.sendMessage(message);
    }
} 